//
//  XMGLoginRegisterViewController.m
//  01-百思不得姐
//
//  Created by xiaomage on 15/7/26.
//  Copyright (c) 2015年 小码哥. All rights reserved.
//

#import "XMGLoginRegisterViewController.h"
#import "XMGTextField.h"
#import "SVProgressHUD.h"

@interface XMGLoginRegisterViewController ()
/** 登录框距离控制器view左边的间距 */
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *loginViewLeftMargin;
@property (weak, nonatomic) IBOutlet XMGTextField *loginPhoneTextFeild;
@property (weak, nonatomic) IBOutlet XMGTextField *loginPwdTextFeild;
@property (weak, nonatomic) IBOutlet XMGTextField *registerPhoneTextFeild;
@property (weak, nonatomic) IBOutlet XMGTextField *registerPwdTextFeild;

@end

@implementation XMGLoginRegisterViewController

- (IBAction)back {
     [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
}
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
}
- (IBAction)loginBtnClick:(id)sender {
    if ([self.loginPhoneTextFeild.text isEqualToString:@""]) {
        [SVProgressHUD showErrorWithStatus:@"请输入手机号"];
        return;
    }
    if (![self validateMobile:self.loginPhoneTextFeild.text]) {
        [SVProgressHUD showErrorWithStatus:@"请输入合法手机号"];
        return;
    }
    if ([self.loginPwdTextFeild.text isEqualToString:@""]) {
        [SVProgressHUD showErrorWithStatus:@"请输入密码"];
        return;
    }
    NSString*name = [[NSUserDefaults standardUserDefaults] objectForKey:@"userPhone"];
    NSString*pwd = [[NSUserDefaults standardUserDefaults] objectForKey:@"userPwd"];
    if ([name isEqualToString:self.loginPhoneTextFeild.text]&&[pwd isEqualToString:self.loginPwdTextFeild.text]) {
        [SVProgressHUD showSuccessWithStatus:@"登录成功"];
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}
- (IBAction)registerBtnClick:(id)sender {
    if ([self.registerPhoneTextFeild.text isEqualToString:@""]) {
        [SVProgressHUD showErrorWithStatus:@"请输入手机号"];
        return;
    }
    if (![self validateMobile:self.registerPhoneTextFeild.text]) {
        [SVProgressHUD showErrorWithStatus:@"请输入合法手机号"];
        return;
    }
    if ([self.registerPwdTextFeild.text isEqualToString:@""]) {
        [SVProgressHUD showErrorWithStatus:@"请输入密码"];
        return;
    }
    [[NSUserDefaults standardUserDefaults] setValue:self.registerPhoneTextFeild.text forKey:@"userPhone"];
    [[NSUserDefaults standardUserDefaults] setValue:self.registerPwdTextFeild.text forKey:@"userPwd"];
    [SVProgressHUD showSuccessWithStatus:@"注册成功"];
}

//手机号码验证
- (BOOL) validateMobile:(NSString *)mobile

{
    //手机号以13， 15，18开头，八个 \d 数字字符
    
    NSString *phoneRegex = @"^((13[0-9])|(15[^4,\\D])|(18[0,0-9]))\\d{8}$";
    NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];
    
    return [phoneTest evaluateWithObject:mobile];
    
}


- (IBAction)showLoginOrRegister:(UIButton *)button {
    // 退出键盘
    [self.view endEditing:YES];
    
    if (self.loginViewLeftMargin.constant == 0) { // 显示注册界面
        self.loginViewLeftMargin.constant = - self.view.width;
        button.selected = YES;
    } else { // 显示登录界面
        self.loginViewLeftMargin.constant = 0;
        button.selected = NO;
    }
    
    [UIView animateWithDuration:0.25 animations:^{
        [self.view layoutIfNeeded];
    }];
}

/**
 * 让当前控制器对应的状态栏是白色
 但是增加了顶部让scrollView滚动的window以后 这个方法不好使了 
 stuteBar的控制就不能交给控制器了 还是要交给application  在infoplist加View controller-based status bar appearance为no
 */
- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
}
@end
